/*
 * =====================================================================================
 *
 *       Filename:  diffsegs.cpp
 *
 *    Description:  Returns maximum number of different segments over
 *                  x and y
 *
 *        Version:  1.0
 *        Created:  05/07/12 11:34:28
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Moos Hueting (mh), mooshueting@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <cv.h>
#include <highgui.h>

using namespace std;
using namespace cv;

float diffsegs(Mat src, char rowcol) {
    Size s = src.size();

    int nSegs;
    bool inSegment;

    if (rowcol == 0) {
        int maxRowSegs = 0;
        // find maximum number of segments per row
        for (int i=0; i < s.height; i++) {
            inSegment = false;
            nSegs = 0;
            for (int j=0; j < s.width; j++) {
                if (src.at<unsigned char>(i,j) != 0) {
                    if (inSegment == false) {
                        nSegs++;
                    }
                    inSegment = true;
                } else {
                    inSegment = false;
                }
            }
            if (nSegs > maxRowSegs)
                maxRowSegs = nSegs;
        }
        return (float)maxRowSegs;
    } else {
    // find maximum number of segments per column
        int maxColSegs = 0;
        for (int i=0; i < s.width; i++) {
            inSegment = false;
            nSegs = 0;
            for (int j=0; j < s.height; j++) {
                if (src.at<float>(j,i) != 0) {
                    if (inSegment == false) {
                        nSegs++;
                    }
                    inSegment = true;
                } else {
                    inSegment = false;
                }
            }
            if (nSegs > maxColSegs)
                maxColSegs = nSegs;
        }
        return (float)maxColSegs;
    }
}

//int main(int argc, char* argv[]) {
//    Mat image;
//
//    image = imread( argv[1], CV_LOAD_IMAGE_GRAYSCALE );
//    threshold(image, image, 100, 255, THRESH_BINARY);
//
//    cout << diffsegs(image, 0) << ", " << diffsegs(image, 1) << endl;
//
//}
